﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Eye.Logic
{
    public class UsersService
    {
        public static void Save(User user)
        {
            using (TheEyeDataContext dc = new TheEyeDataContext())
            {
                dc.Users.InsertOnSubmit(user);
                dc.SubmitChanges();
            }
        }
        
        public static List<User> GetUsers()
        {
            using (TheEyeDataContext dc = new TheEyeDataContext())
            {
                return dc.Users.ToList<User>();
            }
        }

        public static bool RemoveUser(User user)
        {
            bool result = false;
            using (TheEyeDataContext dc = new TheEyeDataContext())
            {
                List<User> user2 = dc.Users.Where(p => p.Passport == user.Passport).Select(p => p).ToList();
                dc.Users.DeleteOnSubmit(user2.First());
                dc.SubmitChanges();
            }
            return result;
        }

        public static bool UpdateUser(User user)
        {
            bool result = false;
            using (TheEyeDataContext dc = new TheEyeDataContext())
            {
                User curUser = dc.Users.Where(p => p.Id == user.Id).First();
                curUser.First_Name = user.First_Name;
                curUser.Second_Name = user.Second_Name;
                curUser.Midl_Name = user.Midl_Name;
                curUser.Passport = user.Passport;
                dc.SubmitChanges();
                result = true;
            }
            return result;
        }

        public static User GetUser (string passport)
        {
            User user = null;
            using (TheEyeDataContext dc = new TheEyeDataContext())
            {
                user = dc.Users.Where(p => p.Passport.Equals(passport)).Select(s => s).First();
            }
            return user;
        }
    }
}
